#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <fcntl.h>
#include <unistd.h>
#include <time.h>


int main()
{
    char str[] = "hello,world,,,,,good,day";
    char str2[] = "hello,world,good,day";
    char *token;

    const int size = 50 * 1024 * 1024;
    char *buf = malloc(size);
    memset(buf, 0, size);
    char *p = buf;


    int fd = open("log", O_RDONLY);
    if (fd < 0) {
        printf("open failed!\n");
        return -1;
    }
    int ret = read(fd, buf, size);
    printf("read: %d\n", ret);

    struct timespec t0, t1;
    double executionTime;
    clock_gettime(CLOCK_MONOTONIC, &t0); // 获取开始时间
    int count = 0;
    while ((token = strtok(p, ",")) != NULL) {
        count++;
        // printf("token: [%s]\n", token);
    }
    clock_gettime(CLOCK_MONOTONIC, &t1); // 获取结束时间

    executionTime = (t1.tv_sec - t0.tv_sec) + (t1.tv_nsec - t0.tv_nsec) / 1e9; // 计算执行时间

    printf("count: %d\n", count);
    printf("timecost: %lf sec\n", executionTime);

    return 0;
}
